//
//  CZHomeViewController.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/2/24.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

class CZHomeViewController: CZBaseTableViewController {

     //创建一个viewModel 的实例
     var statusListViewModel : CZStatusListViewModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        visitor?.setInfo("关注一些人，回这里看看有什么惊喜", image: "visitordiscover_feed_image_house", animation: true)

        // Do any additional setup after loading the view.
        
        //一定要注意必须登陆后才能发送网络请求
        
        if isLogin{
        
            statusListViewModel = CZStatusListViewModel()
            
            loadData()
            
            //注册cell
            tableView.registerClass(CZHomeTableViewCell.self, forCellReuseIdentifier: CZHomeViewControllerReuserIdenfiter)
        //自动计算行高
            tableView.rowHeight = UITableViewAutomaticDimension
            //估算行高 -- 在没有更新我们的约束之前,先预估一个行高
            tableView.estimatedRowHeight = 85
            
            
            //添加到tableView的Footer上
            tableView.tableFooterView = pullView
            
            
            //创建实例添加视图
            tableView.addSubview(refreshView)
            
            refreshView.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
            
            
            
            
        }
        
        
    }

    func loadData(){
        
        SVProgressHUD.show()
    
    //上提更多
        let since_id = statusListViewModel?.array.first?.statusModel?.id ?? 0
        
        //获取max_id
        var max_id = 0
        
        if pullView.isAnimating(){
        
//拿到最后一条数据
            max_id = statusListViewModel?.array.last?.statusModel?.id ?? 0
            max_id -= 1
        }
    
      //  pullView.startAnimating()
        
        statusListViewModel?.loadData(since_id, max_id: max_id, finish: { (count : Int) -> () in
            //还原加载标记
            
            self.tableView.reloadData()
            
            //菊花转动的时候肯定是上提更多
            if !self.pullView.isAnimating(){
            
            //显示刷新数据
                self.showNewCount(count)
            
            }
            
            self.pullView.stopAnimating()
            
            
            SVProgressHUD.dismiss()
            
            self.refreshView.endRefreshing()
            
            
            
        })
        
    }
    
//MARK:--显示刷新提示
    private func showNewCount(count:Int){
        
        //创建视图
        let showMessageLabel = UILabel(frame: CGRectMake(0,64 - 44,kUIScreenWidth,44))
        //设置属性
        showMessageLabel.backgroundColor = UIColor.orangeColor()
        
        if count > 0{
        
        showMessageLabel.text = "更新了\(count)条数据"
        
        }else{
            
            showMessageLabel.text = "暂无更新"
        
        }
    
    showMessageLabel.textAlignment = .Center
        
        showMessageLabel.textColor = UIColor.blackColor()
        
        showMessageLabel.font = UIFont.systemFontOfSize(16)

//MARK:--添加到视图里
        
        self.navigationController?.view.insertSubview(showMessageLabel, belowSubview: (navigationController?.navigationBar)!)
        
        
    UIView.animateWithDuration(2, animations: { () -> Void in
        
        showMessageLabel.transform = CGAffineTransformMakeTranslation(0, 44)
        
        }) { (_) -> Void in
            //当动画完场之后隐藏
            UIView.animateWithDuration(2, animations: { () -> Void in
                //回复到原点CGAffineTransformIdentity
                
                showMessageLabel.transform = CGAffineTransformIdentity
               
                }, completion: { (_) -> Void in
                    
                    //销毁或隐藏
                    showMessageLabel.removeFromSuperview()
            })
        }
    
    
    }
    
    //懒加载
    private lazy var pullView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    private lazy var refreshView: CZRefreshView = CZRefreshView()
    
}


//MARK:--实现数据源方法
extension CZHomeViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel?.array.count ?? 0
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(CZHomeViewControllerReuserIdenfiter, forIndexPath: indexPath) as! CZHomeTableViewCell
        
        cell.statusViewModel = statusListViewModel?.array[indexPath.row]
        
        //需要把cell 传递给cell
        
        //cell.statusViewModel = CZStatusViewModel(model:statusModel!)
//         cell.statusViewModel = statusListViewModel?.array[indexPath.row]
        //cell.textLabel?.text = statusListViewModel?.array[indexPath.row].text
        
        if indexPath.row == (statusListViewModel!.array.count - 1){
        
            //请求数据先转后加载
            
            pullView.startAnimating()
            
            loadData()
            
            print("上啦更多")
         }
        
        
        return cell
    }

}


